package com.nvt.core;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nvt.core.exception.NvtBusinessException;
import com.nvt.core.objectDomain.nvt.Test01;
import com.nvt.core.services.IHibernateService;
import com.nvt.core.services.IJdbcService;
import com.nvt.core.services.ISPService;

@Service
public class ControllerServiceImpl implements IControllerService {

	@Autowired
	private ISPService spService;

	@Autowired
	private IJdbcService jdbcService;

	@Autowired
	private IHibernateService hibernateService;

	@Override
	public void businessMethod() throws NvtBusinessException {
		try {
			Object[] values = new Object[] { "a", "b" };
			spService.executeSpCallback(values);

			// INIT Transaction
			Object[] valuesJdbc = new Object[] { 2, "b", "c", "d", "e" };
			jdbcService.executeInsert("NVT.TEST01", valuesJdbc);

			valuesJdbc = new Object[] { "COLUMN2:5" };
			jdbcService.executeUpdate("NVT.TEST01", valuesJdbc);

			valuesJdbc = new Object[] { "COLUMN1:2" };
			List<Map<String, Object>> map = jdbcService.findByParameters(
					"NVT.TEST01", valuesJdbc);

			valuesJdbc = new Object[] { "COLUMN1:2" };
			jdbcService.executeDelete("NVT.TEST01", valuesJdbc);

			map = jdbcService.findAll("NVT.TEST01");
			// END Transaction

			// INIT HB Transaction
			Test01 test01 = new Test01();
			test01.setColumn1(2);
			test01.setColumn2("column2");
			test01.setColumn3("column3");
			test01.setColumn4("column4");
			test01.setColumn5("column5");

			hibernateService.executeInsert(test01);

			test01 = new Test01();
			test01.setColumn1(2);
			test01.setColumn2("column4");
			test01.setColumn3("column3");
			test01.setColumn4("column2");
			test01.setColumn5("column1");

			hibernateService.executeUpdate(test01);
			// END HB Transaction

		} catch (NvtBusinessException e) {
			throw new NvtBusinessException(e.getMessage());
		}
	}
}
